﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HYJHouseKeeping.Model;

namespace HYJHouseKeeping.DAL
{
    /// <summary>
    /// 订单Service
    /// </summary>
    public class OrderService
    {
        #region
        /// <summary>
        /// 获取所有订单列表
        /// </summary>
        /// <returns>订单集合</returns>
        public List<Order> GetAllOrder()
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                return hyjhouse.Orders.ToList();
            }
        }
        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="order">新增订单对象</param>
        /// <returns>bool</returns>
        public bool AddOrder(Order order) 
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                hyjhouse.Orders.Add(new Order() 
                                            {
                                                OrderID=order.OrderID,
                                                OrderPrice=order.OrderPrice,
                                                CustomerName=order.CustomerName,
                                                CustomerPhone=order.CustomerPhone,
                                                CustomerAddress=order.CustomerAddress,
                                                OrderTime=order.OrderTime,
                                                TypeID=order.TypeID,
                                                EmployeeID=order.EmployeeID,
                                                OrderState=order.OrderState,
                                                Remarks=order.Remarks
                                            });
                if (hyjhouse.SaveChanges()>0)
                {
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        /// 修改订单信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateOrder()
        {
            return false;
        }
        /// <summary>
        /// 根据编号获取订单信息
        /// </summary>
        /// <param name="orderID">订单编号</param>
        /// <returns>订单对象</returns>
        public Order GetOrderById(string orderID)
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
               return hyjhouse.Orders.SingleOrDefault(o=>o.OrderID==orderID);
            }
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool DelOrder(string orderId) 
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                Order order = hyjhouse.Orders.SingleOrDefault(o => o.OrderID == orderId);
                hyjhouse.Orders.Remove(order);
                if (hyjhouse.SaveChanges()>0)
                {
                    result = true;
                }
            }
            return result;
        }

        #endregion
    }
}
